/**
 * Copyright 2015 jianglibo@gmail.com
 *
 */
package com.m3958.logcollector.passwordvalidator;

/**
 * @author jianglibo@gmail.com
 *         2015年12月25日
 *
 */
public class PasswordValidator {
    private final int minLength;
    private final int maxLength;
    
    private final PasswordValidateMethod[] validators;

    public PasswordValidator(int minLength, int maxLength, PasswordValidateMethod...validators) {
        this.minLength = minLength;
        this.maxLength = maxLength;
        this.validators = validators;
    }

    public void validate(String password) throws PasswordValidatorException {
        if (password == null) {
            throw new PasswordValidatorException("EMPTY_PASSWORD");
        }
        checkLength(password);
        for(PasswordValidateMethod pvm: validators) {
            pvm.validate(password);
        }
    }


    private void checkLength(String password) throws PasswordValidatorException {
        if (password.length() < getMinLength()) {
            throw new PasswordValidatorException("MIN_LENGTH");
        }
        if (password.length() > getMaxLength()) {
            throw new PasswordValidatorException("MAX_LENGTH");
        }
    }

    public int getMinLength() {
        return minLength;
    }

    public int getMaxLength() {
        return maxLength;
    }
}
